﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NFinalCompiler.SqlModel
{
    public class ModelHelper
    {
        public static string GetName(string v)
        {
            string a = v;
            a = a.Substring(0, 1).ToUpper() + a.Substring(1);
            return a;
        }
        public static string GetPrivateName(string v)
        {
            return "_" + GetName(v).ToLower();
        }
        public static System.Text.RegularExpressions.Regex enumLinkReg = new System.Text.RegularExpressions.Regex("enum\\(\\s*(\\S+)\\s*\\)");
        public static string GetColType(DatabaseSchemaReader.DataSchema.DatabaseColumn col,string enumNameSpace)
        {
            string typeName = col.DataType.TypeName.ToLower();
            string csharpName = col.DataType.NetDataTypeCSharpName;
            if (typeName == "bit")
            {
                csharpName = "bool";
            }
            if (csharpName == "int" || csharpName == "short" || csharpName=="byte" || csharpName=="long")
            {
                if (col.Description != null)
                {
                    var match = enumLinkReg.Match(col.Description);
                    if (match.Success)
                    {
                        csharpName = enumNameSpace + "." + match.Groups[1].Value;
                    }
                }
            }
            if (col.Nullable && (!col.DataType.IsString))
            {
                csharpName += "?";
            }
            return csharpName;
        }

        public static string GetTableName(string tableName)
        {
            string[] tableNameArray = tableName.Split('_');
            string a = "";
            for (int i = 0; i < tableNameArray.Length; i++)
            {
                if (i == 0 && tableNameArray[0].Length < 2)
                {
                    continue;
                }
                else
                {

                    a += char.ToUpper(tableNameArray[i][0]) + tableNameArray[i].Substring(1);
                }
            }
            return a;
        }

        public static string GetTableName(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string tableName = table.Name;
            return GetTableName(tableName);
        }
        public static string GetCol(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {
                    if (i > 0)
                    {
                        sql += ",[" + m.Name + "]";
                    }
                    else
                    {
                        sql += "[" + m.Name + "]";
                    }
                    if (m.DataType.NetDataTypeCSharpName == "geography")
                    {
                        sql += ".ToString()" + m.Name;
                    }
                    i++;
                }

            }

            return sql;
        }
        public static string GetColAll(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {

                if (i > 0)
                {
                    sql += ",[" + m.Name + "]";
                }
                else
                {
                    sql += "[" + m.Name + "]";
                }

                i++;
            }

            return sql;
        }
        public static string GetColp(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {

                    if (i > 0)
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += ",geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += ",@" + m.Name;
                        }

                    }
                    else
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += "geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += "@" + m.Name;
                        }
                    }

                    i++;
                }

            }

            return sql;
        }
        public static string GetColu(DatabaseSchemaReader.DataSchema.DatabaseTable table)
        {
            string sql = "";
            int i = 0;
            foreach (var m in table.Columns)
            {
                if (!m.IsPrimaryKey)
                {
                    if (i > 0)
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += "," + m.Name + "=geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += "," + m.Name + "=@" + m.Name;
                        }

                    }
                    else
                    {
                        if (m.DataType.NetDataTypeCSharpName == "geography")
                        {
                            sql += m.Name + "=geography::STGeomFromText(@" + m.Name + ", 4326)";
                        }
                        else
                        {
                            sql += m.Name + "=@" + m.Name;
                        }

                    }

                    i++;
                }

            }

            return sql;
        }
        
        public static string FiledDefaultValue(DatabaseSchemaReader.DataSchema.DatabaseColumn cs)
        {
            if (cs.IsPrimaryKey) return ";";
            if (cs.DefaultValue == null)
            {
                return ";";
            }
            string def = cs.DefaultValue.Trim('(',')','\'','b');

            if (string.IsNullOrEmpty(def))
            {
                string v = "";
                var dbType = cs.DataType.NetDataTypeCSharpName;
                if (string.Compare(cs.DataType.TypeName, "bit", true) == 0)
                {
                    dbType = "bool";
                }
                if (cs.DataType.NetDataTypeCSharpName == "geography") return v = "=\"POINT (0 0)\";";
                switch (dbType)
                {
                    case "string":
                        v = " = \"\";";
                        break;
                    case "DateTime":
                        v = " = new DateTime(1900, 1, 1);";
                        break;
                    case "TimeSpan":
                        v = " = new TimeSpan();";
                        break;
                    case "bool":
                        v = " = false;";
                        break;
                    case "decimal":
                        v = " = 0m;";
                        break;
                    default:
                        v = " = 0;";
                        break;

                }
                return v;
            }
            else
            {
                string v = "";
                var dbType = cs.DataType.NetDataTypeCSharpName;
                if (string.Compare(cs.DataType.TypeName, "bit", true) == 0)
                {
                    dbType = "bool";
                }
                switch (dbType)
                {
                    case "string":
                        v = "\"" + def + "\";";
                        break;
                    case "DateTime":
                        if (def.IndexOf("getdate") >= 0)
                        {
                            v = "DateTime.Now;";
                        }
                        else
                        {
                            v = "Convert.ToDateTime(\"" + def + "\");";
                        }
                        break;
                    case "TimeSpan":
                        string[] vals = def.Split(':');
                        v = string.Format("new TimeSpan({0},{1},{2});", vals[0], vals[1], vals[2]);
                        break;
                    case "bool":
                        if (def == "0")
                        {
                            v = "false;";
                        }
                        else
                        {
                            v = "true;";
                        }
                        break;
                    case "decimal":
                        v = def + "M;";
                        break;
                    case "float":
                        v = def + "f;";
                        break;
                    default:
                        v = "" + def + ";";
                        break;

                }
                return " = " + v;
            }

        }
    }
}
